package cn.lwy.example.niit_race.controller;

import cn.lwy.example.niit_race.pojo.Matter;
import cn.lwy.example.niit_race.service.MatterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class RouteController {
    @Autowired
    MatterService matterService;

    @RequestMapping("/level1/1")
    public String getMatter(Model model){
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        User user = (User) auth.getPrincipal();
        String name = user.getUsername().toString();
        model.addAttribute("matters", matterService.getMatterByUserId(matterService.getIdByUsername(name)));
        return "views/level1/1";
    }

    @RequestMapping("/updateMatter/{id}")
    public String updateMatter(Model model,@PathVariable("id")int id){
        Matter matter = matterService.getMatterById(id);
        model.addAttribute("matter",matter);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = df.format(matter.getDeadline());

        model.addAttribute("day",date.split(" ")[0]);
        model.addAttribute("time",date.split(" ")[1]);
        return "views/level1/update";
    }

    @PostMapping("/updateMatter")
    public void update(Matter matter, String deadline_day, String deadline_time) throws ParseException {
        String deadline_str = deadline_day + " " + deadline_time + ":00";
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date deadline = s.parse(deadline_str);
        matter.setDeadline(deadline);

        matterService.updateMatter(matter);
        //return "redirect:/level1/1";
    }

    @RequestMapping("/addMatter")
    public void addMatter(Matter matter, String deadline_day, String deadline_time) throws ParseException {
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        User user = (User) auth.getPrincipal();
        String name = user.getUsername().toString();

        String deadline_str = deadline_day + " " + deadline_time + ":00";
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date deadline = s.parse(deadline_str);
        matter.setDeadline(deadline);
        matter.setAuthorId(matterService.getIdByUsername(name));
        matter.setFinish(0);
        matter.setRm(0);
        matter.setSign(0);
        matterService.addMatter(matter);
        //return "redirect:/level1/1";
    }

    @RequestMapping("/level1/2")
    public String toAddMatter(){
        return "views/level1/2";
    }

    @RequestMapping("/delMatter/{id}")
    public String deleteMatter(@PathVariable("id")int id){
        matterService.delMatter(id);
        return "redirect:/level1/1";
    }

//    @RequestMapping("/level1/{id}")
//    public String level1(@PathVariable("id")int id){
//        return "views/level1/" + id;
//    }

    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id")int id){
        return "views/level2/" + id;
    }

    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id")int id){
        return "views/level3/" + id;
    }


    /////////////////////////////////////////////////////////////

}
